package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.exception.ApiException;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizDepartment;
import cn.sfrog.project.biz.admin.domain.vo.BizDepartmentFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDepartmentQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDepartmentVO;
import cn.sfrog.project.biz.admin.mapper.BizDepartmentMapper;
import cn.sfrog.project.biz.admin.service.IBizDepartmentService;
import cn.sfrog.project.biz.admin.service.IBizDoctorService;
import cn.sfrog.project.biz.web.vo.index.*;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 科室Service业务层处理
 */
@Service
public class BizDepartmentServiceImpl extends ServiceImpl<BizDepartmentMapper, BizDepartment> implements IBizDepartmentService {
    @Resource
    private BizDepartmentMapper bizDepartmentMapper;
    @Autowired
    private IBizDoctorService bizDoctorService;

    /**
     * 查询科室
     *
     * @param id 科室主键
     * @return 科室
     */
    @Override
    public BizDepartmentVO selectBizDepartmentById(Long id) {
        return bizDepartmentMapper.selectBizDepartmentById(id);
    }

    /**
     * 查询科室列表
     *
     * @param query 科室
     * @return 科室
     */
    @Override
    public List<BizDepartmentVO> selectBizDepartmentList(BizDepartmentQueryVO query) {
        return bizDepartmentMapper.selectBizDepartmentList(query);
    }

    /**
     * 新增科室
     *
     * @param form 科室
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizDepartment(BizDepartmentFormVO form) {
        LambdaQueryWrapper<BizDepartment> queryWrapper = new LambdaQueryWrapper<BizDepartment>()
                .eq(BizDepartment::getHospitalId,form.getHospitalId())
                .eq(BizDepartment::getDepartmentName,form.getDepartmentName());
        BizDepartment dbEntity = BizDepartment.DAO.selectOne(queryWrapper);
        if(StringUtils.isNotNull(dbEntity)){
            throw new ApiException("该医院下已经存在相同科室,不可重复添加");
        }
        BizDepartment bizDepartment = new BizDepartment();
        BeanUtils.copyProperties(form, bizDepartment);
        bizDepartment.setCreateTime(new Date());
        bizDepartment.setCreateBy(SecurityUtils.getUsername());
        bizDepartment.setUpdateTime(new Date());
        bizDepartment.setUpdateBy(SecurityUtils.getUsername());
        return bizDepartment.insert();
    }

    /**
     * 修改科室
     *
     * @param form 科室
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizDepartment(BizDepartmentFormVO form) {
        BizDepartment bizDepartment = new BizDepartment();
        BeanUtils.copyProperties(form, bizDepartment);
        bizDepartment.setUpdateTime(new Date());
        bizDepartment.setUpdateBy(SecurityUtils.getUsername());
        return bizDepartment.updateById();
    }

    /**
     * 批量删除科室
     *
     * @param ids 需要删除的科室主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizDepartmentByIds(Long[] ids) {
            return BizDepartment.DAO.delete(Wrappers.lambdaQuery(BizDepartment.class).in(BizDepartment::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return BizDepartment.DAO.update(Wrappers.lambdaUpdate(BizDepartment.class).in(BizDepartment::getId, (Object[]) vo.getIds()).set(BizDepartment::getStatus, vo.getStatus()));
    }

    @Override
    public List<AppDepartmentResultVO> selectAppDepartmentListByHospitalId(Long id) {
        return bizDepartmentMapper.selectAppDepartmentListByHospitalId(id);
    }

    @Override
    public AppDepartmentDetailResultVO selectAppDepartmentDetail(Long id) {
        AppDepartmentDetailResultVO detailResultVO = bizDepartmentMapper.selectAppDepartmentDetail(id);
        if(StringUtils.isNotNull(detailResultVO)){
            //查询科室下的所有医生
            List<AppDoctorResultVO> doctorResultVOList =  bizDoctorService.selectAppDoctorListByDepartmentId(id);
            detailResultVO.setDoctorList(doctorResultVOList);
        }
        return detailResultVO;
    }

    @Override
    public List<AppDepartmentNameResultVO> selectAppAllDepartmentList() {
        List<AppDepartmentNameResultVO> departmentNameResultVOS = bizDepartmentMapper.selectAppAllDepartmentList();
        return departmentNameResultVOS;
    }

    @Override
    public List<AppDepartmentNameResultVO> selectAppAllDepartmentListByCityCode(String cityCode) {
        List<AppDepartmentNameResultVO> departmentNameResultVOS = bizDepartmentMapper.selectAppAllDepartmentListByCityCode(cityCode);
        return departmentNameResultVOS;
    }

    /**
     * 查询一级科室列表
     * @param hospitalId
     * @return
     */
    @Override
    public List<AppFirstDepartmentNameResultVO> getFirstDepartmentList(Long hospitalId) {
        List<AppFirstDepartmentNameResultVO> firstDepartmentNameResultVOS = bizDepartmentMapper.getFirstDepartmentList(hospitalId);
        return firstDepartmentNameResultVOS;
    }

    /**
     * 查询二级科室列表
     * @param parentQueryVO
     * @return
     */
    @Override
    public List<AppDepartmentNameResultVO> getSecondDepartmentList(AppDepartmentParentQueryVO parentQueryVO) {
        List<AppDepartmentNameResultVO> secondDepartmentList = bizDepartmentMapper.getSecondDepartmentList(parentQueryVO);
        return secondDepartmentList;
    }
}
